<?php
error_reporting(0);

// $shortopts  = "";
// $shortopts .= "f:";  // Required value
// $shortopts .= "v::"; // Optional value
// $shortopts .= "abc"; // These options do not accept values
// $longopts  = array(
//     "required:",     // Required value
//     "optional::",    // Optional value
//     "option",        // No value
//     "opt",           // No value
// );
$shortopts  = "";
$longopts = array(
  "host::",
  "port::"
);

$options = getopt($shortopts,$longopts);
!isset($options["host"]) && $options["host"] = "127.0.0.1";
!isset($options["prot"]) && $options["port"] = "8085"; 

$socket = stream_socket_server("tcp://".$options["host"].":".$options["port"], $errno, $errstr);
if (!$socket) {
  echo "$errstr ($errno)",PHP_EOL;
} else {
  echo "tcp run on ".$options["host"].":".$options["port"],PHP_EOL;
//设置非阻塞模式的
//stream_set_blocking($socket,0);

$conns = array();
$timeout = 30;
$on = true;

$read = array();
$clients = array();
$max_clients = 3;
$test = true;

do{
      $read[0] = $socket;

      //获取监听的列表
      for($i = 0; $i < $max_clients; $i++)
      {
        if(isset($clients[$i]))
        {
          $read[$i + 1] = $clients[$i];
        }
      }
      //echo "stream_select before",PHP_EOL;

      //监听是否有数据写入??
      $write = NULL;
      $except = NULL;
      $tv_sec = 5;
      if(stream_select($read, $write, $except, $tv_sec) < 1)
      {
        continue;
      }
      //echo "stream_select",PHP_EOL;

      //创建客户端的
      if(in_array($socket, $read))
      {
        for($i = 0; $i < $max_clients; $i++)
        {
          if(empty($clients[$i]))
          {
            $clients[$i] = stream_socket_accept($socket,-1);
            //echo "stream_socket_accept",PHP_EOL;
            break;
          }
          elseif($i == ($max_clients-1))
          {
            echo("Too many clients... :( ");
          }
        }

      }

      //客户端有数据写入的
      //echo count($clients),PHP_EOL;
      for($i = 0; $i < $max_clients; $i++)
      {
        if(isset($clients[$i]))
        {
          if(in_array($clients[$i], $read))
          {

            stream_copy_to_stream($clients[$i],$clients[$i]);
/*              $header = fread($clients[$i],10);
              echo "header:",$header,PHP_EOL;
              $size = intval($header);
              if($size > 0){
                $body = fread($clients[$i],$size);
              }else{
                $body = "";
              }
              echo "body:",$body,PHP_EOL;

              sleep(2);
              echo "write",PHP_EOL;

              $body_r = strrev($body);
              $header_r = str_pad(strlen($body_r),10," ",STR_PAD_LEFT);
              $msg_r = $header_r.$body_r;

              fwrite($clients[$i],$msg_r);*/

            fclose($clients[$i]);
            /*
            $input = fread($clients[$i],2);
            echo "fread",PHP_EOL;
            if($input == null)
            {
              fclose($clients[$i]);
              echo "fclose",PHP_EOL;
            }
            else
            {
              echo($input);
              fwrite($clients[$i],$input);
              echo "fwrite",PHP_EOL;
              fclose($clients[$i]);
              echo "fclose",PHP_EOL;
            }*/
          }
        }
      }

}while($test);
fclose($socket);
}
// do{
//     $conn = stream_socket_accept($socket,-1);
//     stream_set_blocking($conn,0);
//     if(in_array($conn,$conns)){

//     }
//     echo "connected!",PHP_EOL;
//     sleep(3);
//     fclose($conn);
//     //$on = false;
//   }while ($on);
//   fclose($socket);
// }

// function handle($conn){
//       //$contents = stream_get_contents($conn);
//     //echo "recive msg".$contents,PHP_EOL;
//     //$contents = "this is a test";
//     $contents = "";
//     while(!feof($conn)){
//       echo $contents .= fgets($conn,1024);
//     }
//     fwrite($conn, $contents);
//     fclose($conn);
// }